package com.hongyi.demo.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hongyi.demo.pojo.User;
import com.hongyi.demo.pojo.dto.UserFormDTO;
import com.hongyi.demo.pojo.query.UserQuery;
import com.hongyi.demo.pojo.vo.UserVO;
import com.hongyi.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hongyi
 * @date 2024/7/22 18:10
 */

@Api(tags = "用户管理接口")
//@RequiredArgsConstructor
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private  IUserService userService;

    @PostMapping
    @ApiOperation("新增用户")
    public void saveUser(@RequestBody UserFormDTO userFormDTO){
        // 1.转换DTO为PO
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        // 2.新增
        userService.save(user);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public void removeUserById(@PathVariable("id") Long userId){
        userService.removeById(userId);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public UserVO queryUserById(@PathVariable("id") Long userId){
        // 1.查询用户
        User user = userService.getById(userId);
        // 2.处理vo
        return BeanUtil.copyProperties(user, UserVO.class);
    }

    @GetMapping
    @ApiOperation("根据id集合查询用户")
    public List<UserVO> queryUserByIds(@RequestParam("ids") List<Long> ids){
        // 1.查询用户
        List<User> users = userService.listByIds(ids);
        // 2.处理vo
        return BeanUtil.copyToList(users, UserVO.class);
    }


    /**
     * @param id
     * @param money
     */
    @PutMapping("{id}/deduction/{money}")
    @ApiOperation("扣减用户余额")
    public void deductBalance(@PathVariable("id") Long id, @PathVariable("money")Integer money){
        userService.deductBalance(id, money);
    }

    /*@GetMapping("/list")
    @ApiOperation("根据id集合查询用户")
    public List<UserVO> queryUser(UserQuery query) {
        // 1. 组织条件
        *//**
         * 获取前端传过来的数据
         *//*
        String username = query.getName();
        Integer status = query.getStatus();
        Integer minBalance = query.getMinBalance();
        Integer maxBalance = query.getMaxBalance(); // lambda
        LambdaQueryWrapper<User> wrapper = new QueryWrapper<User>().lambda()
                .like(username !=null, User::getUsername, username)
                .eq(status !=null, User::getStatus,status)
                .ge(minBalance !=null, User::getBalance,maxBalance)
                .le(maxBalance !=null, User::getBalance, maxBalance);

         // 2. 查询用户
        List<User> users = userService.list(wrapper);
        // 3. 处理vo
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
        return userVOS;
    }*/

    @GetMapping("/list")
    @ApiOperation("根据id集合查询用户")
    public List<UserVO> queryUser(UserQuery query) {
        // 1. 组织条件

        /**
         * 获取前端传过来的数据
         */

        String username = query.getName();
        Integer status = query.getStatus();
        Integer minBalance = query.getMinBalance();
        Integer maxBalance = query.getMaxBalance(); // lambda

        // 2.查询用户
        List<User> users = userService.lambdaQuery()
                .like(username !=null, User::getUsername, username)
                .eq(status !=null, User::getStatus, status)
                .ge(minBalance !=null, User::getBalance, minBalance)
                .le(maxBalance !=null, User::getBalance,maxBalance)
                .list();
        // 3. 处理vo
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
        return userVOS;
    }



}
